﻿/* *************************************************
 *  Copyright © FlexDev 2010. All rights reserved.
 *  Author: Rodrigo da Silva Brito
 *  Date: 07/16/2010
 *  Contact: mailto:rodrigosilvabrito@hotmail.com
 *  Site: http://www.flexdev.com.br
 * *************************************************
*/
using System.ServiceModel.Description;

namespace Flexdev.Wcf.Extensions.Silverlight
{
    public class FlexdevBehavior : IEndpointBehavior
    {
        #region IEndpointBehavior Members

        public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
        {
        }

        public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
        {
            string userName = string.Empty;
            string password = string.Empty;

            foreach (IEndpointBehavior behavior in endpoint.Behaviors)
            {
                ClientCredentials clientCredentials = behavior as ClientCredentials;
                if (clientCredentials != null)
                {
                    userName = clientCredentials.UserName.UserName;
                    password = clientCredentials.UserName.Password;
                    break;
                }
            }

            FlexdevMessageInspector inspector = new FlexdevMessageInspector(userName, password);
            clientRuntime.MessageInspectors.Add(inspector);
        }

        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
        {
        }

        public void Validate(ServiceEndpoint endpoint)
        {
        }

        #endregion
    }
}
